using System.Collections.ObjectModel;
using HMCollada.Core;
using System.Xml;

namespace HMCollada.Core {
    internal struct HMColladaTriangles {
        internal string name;
        internal int count;
        internal string material;
        internal Collection<HMColladaInput> input;
        internal int[] p;
    }
}

namespace HMCollada {
    internal partial class HMColladaLoader {
        private static HMColladaTriangles LoadTriangles() {
            HMColladaTriangles newTriangles = new HMColladaTriangles();
            newTriangles.name = colladaReader.GetAttribute("name");
            newTriangles.count = int.Parse(colladaReader.GetAttribute("count"));
            newTriangles.material = colladaReader.GetAttribute("material");

            newTriangles.input = new Collection<HMColladaInput>();

            if (!colladaReader.IsEmptyElement) colladaReader.Read();
            do {
                if (colladaReader.NodeType != XmlNodeType.EndElement) {
                    switch (colladaReader.Name) {
                        case "input": newTriangles.input.Add(LoadInput()); break;
                        case "p":
                            string content = ReadString().Trim();
                            while (content.Contains("  ")) content = content.Replace("  ", " ");
                            string[] values = content.Split(new char[] { ' ' });
                            newTriangles.p = new int[values.Length];
                            for (int i = 0; i < newTriangles.count; i++) {
                                newTriangles.p[i] = int.Parse(values[i]);
                            }
                            break;
                    }
                }

                if (!colladaReader.IsEmptyElement) colladaReader.Read();
            } while (colladaReader.Name != "triangles");

            if (colladaReader.IsEmptyElement) colladaReader.Read();
            return newTriangles;
        }
    }
}